package xyz.feiwo.singleton;

/**
 * Created by fei wo at 2021/4/21.
 *
 * @author fei wo <a mailto:feiwodev@foxmail.com>feiwodev@foxmail.com</a>
 * @since 1.0
 *
 * 饿汉式单例模式
 */
public class HungryTypeSingleton {

    /**
     * 使用static 在类加载的时候就创建对象， JVM保证每次loadClass都只有一个，保证了当前单例模式是线程安全的
     */
    private static final HungryTypeSingleton INSTANCE = new HungryTypeSingleton();

    /**
     * 禁止外部使用new创建对象
     */
    private HungryTypeSingleton(){}

    public static HungryTypeSingleton getInstance() {
        return INSTANCE;
    }

    public static void main(String[] args) {
        HungryTypeSingleton singleton01 = HungryTypeSingleton.getInstance();
        HungryTypeSingleton singleton02 = HungryTypeSingleton.getInstance();

        System.out.println(singleton01 == singleton02);
    }
}
